﻿package chuichui.Utils
{
	import flash.display.*;
	import flash.events.*;
	import flash.filters.GlowFilter;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	import flash.utils.ByteArray;

	/**
	 * 屏幕打印
	 * @author Michael
	 *
	 */
	public class Print extends Sprite
	{

		/**
		 * 输入文本
		 */
		private var inputTxt : TextField;
		/**
		 * -----------------------------------------------功能配置-------------------------------------------------------
		 * 屏幕打印解锁密码
		 */
		public static var PASS_WORD : String = "PABCD";

		/**
		 * 最多显示行数
		 */
		public static var MAX_LINE_COUNT : int = 37;

		/**
		 * 连接符
		 */
		public static const SPLITTER : String = "\t";

		/**
		 * 字体大小
		 */
		public static var fontSize : int = 12;

		/**
		 * 字体样式
		 */
		public static var fontFamily : String = "Lucida Sans Unicode";

		/**
		 * 行距
		 */
		public static var lineSpacing : int = 1;

		/**
		 * 背景颜色
		 */
		public static var backgroundColor : int = 0x000000;

		/**
		 * 背景透明度
		 */
		public static var backgroundAlpha : Number = 0;

		/**
		 * 默认启动打印屏幕
		 */
		public static var defaultStart : Boolean = true;

		/**
		 * 隐藏后是否保存信息
		 */
		public static var saveOnHide : Boolean = true;

		/**
		 *  ----------------------------------------------颜色配置-----------------------------------------------------
		 * 一般信息颜色
		 */
		public static const NORMAL_INFOR_COLOR : int = 0xFFFFFF;
		/**
		 * 大厅信息颜色
		 */
		public static const HALL_INFOR_COLOR : int = 0x00FFFF;
		/**
		 * 房间信息颜色
		 */
		public static const ROOM_INFOR_COLOR : int = 0x66FF00;
		/**
		 * SOCKET信息颜色
		 */
		public static const SOCKET_INFOR_COLOR : int = 0xFFFF00;
		/**
		 * PHP信息颜色
		 */
		public static const PHP_INFOR_COLOR : int = 0x3300FF;
		/**
		 * 错误信息颜色
		 */
		public static const ERROR_INFOR_COLOR : int = 0xFF0000;
		/**
		 * 警告颜色
		 */
		public static const WARN_INFOR_COLOR : int = 0xFFFF00;
		/**
		 * 描黑滤镜
		 */
		public static var blackFilters : Array = [new GlowFilter(0x000000, 1, 2, 2, 255)];
		/**
		 * 输出文本
		 */
		private var printField : TextField;
		private var _infor : String = "";
		/**
		 * 如果输入的字符和PASS_WORD的第一个数字一样，则重新开始记录输入
		 * 看输入是否和PASS_WORD一样。一样则开启打印
		 */
		private var startRecord : Boolean;
		/**
		 * 输入的字母
		 */
		private var inputWords : String = "";
		/**
		 * 是否可以使用
		 */
		private var isAvailable : Boolean;
		/**
		 * 显示容器
		 */
		private var container : Stage;

		/**
		 * ----------------------------------------------静态方法------------------------------------------------------
		 */
		private static var instance : Print;

		/**
		 * 初始化设置舞台后才能使用
		 * @param	state
		 */
		public static function init(stage : Stage) : void
		{
			if(!instance)
			{
				instance = new Print(stage);
			}
		}

		public function Print(stage : Stage) : void
		{
			this.container = stage;
			this.init();
		}

		/**
		 * 初始化
		 */
		private function init() : void
		{
			this.graphics.beginFill(backgroundColor, backgroundAlpha);
			this.graphics.drawRect(0, 0, container.stageWidth, container.stageHeight);
			this.graphics.endFill();

			this.mouseEnabled = false;
			//this.mouseChildren = false;

			printField = new TextField();
			printField.width = container.stageWidth;
			printField.height = container.stageHeight;
			printField.selectable = false;
			printField.mouseEnabled = false;
			printField.wordWrap = true;
			printField.text = "";
			printField.filters = blackFilters;

			this.addChild(printField);

			container.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler, false, 0, true);

			inputTxt = new TextField();
			var format : TextFormat = new TextFormat();
			format.size = 14;
			format.bold = true;
			format.font = "宋体";
			inputTxt.defaultTextFormat = format;
			inputTxt.background = true;
			inputTxt.backgroundColor = 0x000000;
			inputTxt.textColor = 0xffffff;
			inputTxt.type = TextFieldType.INPUT;
			inputTxt.width = container.stageWidth * .5;
			inputTxt.height = 20;
			inputTxt.x = (container.stageWidth - inputTxt.width) * .5;
			inputTxt.y = (container.stageHeight - 100)
			inputTxt.wordWrap = true;
			this.addChild(inputTxt);
			inputTxt.visible = false;
			inputTxt.alpha = 0.6;
			inputTxt.filters = blackFilters;

			if(defaultStart)
			{
				showPrinter();
			}
		}

		/**
		 * 打印对象
		 * @param	target		对象主体
		 * @param	delimiter	划分格式
		 * @return
		 */
		public static function printObject(target : Object, delimiter : String = "") : String
		{
			var obj : Object;
			var str : String = "\n";
			var objStr : String = "";

			for(var key : String in target)
			{
				objStr = "";
				obj = target[key];

				if(!(obj is int || obj is String || obj is Number || obj is uint))
				{
					objStr = printObject(obj, delimiter + "\t");
				}
				str += delimiter + key + " => " + objStr + "\n";
			}

			str = str.replace(/(\n){2}/g, "$1");
			return str;
		}

		/**
		 * 大厅打印
		 * @param	...arg
		 */
		public static function hallPrint(... arg) : void
		{
			write(arg, HALL_INFOR_COLOR);
		}

		/**
		 * 房间打印
		 * @param	..arg
		 */
		public static function roomPrint(... arg) : void
		{
			write(arg, ROOM_INFOR_COLOR);
		}

		/**
		 * SOCKET打印
		 * @param	...arg
		 */
		public static function socketPrint(... arg) : void
		{
			write(arg, SOCKET_INFOR_COLOR);
		}

		/**
		 * PHP打印
		 * @param	...arg
		 */
		public static function phpPrint(... arg) : void
		{
			write(arg, PHP_INFOR_COLOR);
		}

		/**
		 * 错误信息打印
		 * @param	...arg
		 */
		public static function error(... arg) : void
		{
			write(arg, ERROR_INFOR_COLOR);
		}

		/**
		 * 警告
		 * @param arg
		 *
		 */
		public static function warning(... arg) : void
		{
			write(arg, WARN_INFOR_COLOR);
		}

		/**
		 * 普通打印
		 * @param	...arg
		 */
		public static function print(... arg) : void
		{
			write(arg, NORMAL_INFOR_COLOR);
		}

		/**
		 * 替换打印
		 * @param arg
		 * printf("aaa,%d,hi",bbb) 输出 aaa,bbb,hi
		 */
		public static function printf(str : String, ... arg) : String
		{
			for each(var tmp : String in arg)
			{
				str = str.replace("%d", tmp);
			}
			return str;
		}

		/**
		 * 屏幕打印
		 * @param	inforList	信息列表
		 * @param	color		颜色
		 */
		private static function write(inforList : Array, color : int) : void
		{
			if(!instance)
			{
				//throw(new Error("打印器没有初始化，应该先初始化设置舞台后方能使用！"));
				return;
			}

			var infor : String = inforList.join(SPLITTER);
			instance.write("<font color='#" + color.toString(16) + "'>\\&gt;" + infor + "</font>");
		}

		/*************************************类属性方法************************************************/

		/**
		 * 测试
		 * @param	...arg
		 */
		public function write(infor : String) : void
		{
			if(!isAvailable && !saveOnHide)
			{
				return;
			}

			_infor += infor + "\n";

			if(printField.numLines > MAX_LINE_COUNT)
			{
				_infor = _infor.replace(/.*\n/, "");
			}

			printField.htmlText = formatText(_infor);
		}

		/**
		 * 格式化文本
		 * @param	content
		 * @return
		 */
		private function formatText(content : String) : String
		{
			var left : String = "<font face='" + fontFamily + "' size='";
			left += fontSize + "'><textFormat leading='"
			left += lineSpacing + "'>";

			var right : String = "</textFormat></font>"
			return left + content + right;
		}

		public function set version(ver : String) : void
		{
			var txt : TextField = new TextField();
			txt.text = "V-" + ver;
			this.addChild(txt);
			txt.x = 760 - txt.textWidth;
			txt.y = 610 - txt.textHeight;
			txt.textColor = 0xff0000;
		}


		public static function set version(ver : String) : void
		{
			instance.version = ver;
		}

		/**
		 * 键盘事件
		 * @param	e
		 */
		private function keyUpHandler(e : KeyboardEvent) : void
		{
//			switch(e.keyCode)
//			{
//				case Keycode.Esc:
//					hidePrinter();
//					break;
//				case Keycode.Delete:
//					clearPrinter();
//					break;
//			}

			//检测是否弹出和发送命令框
//			if(isAvailable && e.keyCode == Keycode.Enter)
//			{
//				inputHandler(e.ctrlKey);
//			}

			//打印设置不启用，才检测是否需要开启检测功能
			if(!isAvailable)
			{
				checkShowPriner(e.keyCode);
			}
		}

		/**
		 * 输入处理
		 * @param keyCode
		 * @param ctrlKey
		 *
		 */
//		private function inputHandler(ctrlKey : Boolean) : void
//		{
//			if(ctrlKey)
//			{
//				inputTxt.visible = !inputTxt.visible;
//				stage.focus = inputTxt;
//			}
//			else
//			{
//				if(inputTxt.text == "")
//				{
//					inputTxt.visible = false;
//					return;
//				}
//				var bytes : ByteArray = new ByteArray();
//				bytes.writeUTF(inputTxt.text);
//				GameSocket.instance.send(10000, bytes);
//				inputTxt.text = "";
//				stage.focus = inputTxt;
//			}
//		}

		/**
		 * 检测是否开启打印
		 * @param keyCode
		 *
		 */
		private function checkShowPriner(keyCode : uint) : void
		{
			if(keyCode == PASS_WORD.charCodeAt(0))
			{
				startRecord = true;
				inputWords = "";
			}

			if(startRecord && inputWords.length < PASS_WORD.length)
			{
				inputWords += String.fromCharCode(keyCode);

				if(inputWords == PASS_WORD)
				{
					showPrinter();
				}
			}
			else
			{
				startRecord = false;
			}
		}

		/**
		 * 清空打印屏幕
		 */
		private function clearPrinter() : void
		{
			_infor = "";
			printField.htmlText = "";
		}

		/**
		 * 显示打印机
		 */
		private function showPrinter() : void
		{
			isAvailable = true;

			if(this.parent == null)
			{
				container.addChild(this);
			}
		}

		/**
		 * 隐藏打印机
		 */
		private function hidePrinter() : void
		{
			isAvailable = false;

			if(!saveOnHide)
				clearPrinter();

			this.parent && this.parent.removeChild(this);
		}

	}

}